/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package player;

import java.sql.*;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import javax.ejb.*;
import team.Reflectable;

/**
 *
 * @author denaas
 */
public class HockeyPlayerBean extends PlayerBean implements EntityBean {

    private HockeyPlayer getHockeyPlayer() {
        return (HockeyPlayer) getPlayer();
    }

    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">
    /**
     * @see javax.ejb.EntityBean#ejbRemove()
     */
    public void ejbRemove() {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("DELETE FROM hockeyplayers WHERE id = ?");
            stmt.setInt(1, getHockeyPlayer().getId().intValue());

            if (stmt.executeUpdate() < 1) {
                throw new NoSuchEntityException();
            }


            super.ejbRemove();
        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }

    /**
     * @see javax.ejb.EntityBean#ejbLoad()
     */
    public void ejbLoad() {
        Object key = getContext().getPrimaryKey();
        if (key instanceof String) {
            getPlayer().setId(new Integer((String) key));
        } else if (key instanceof Integer) {
            getPlayer().setId((Integer) key);
        }

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("Select * from hockeyplayers where id = ?");
            stmt.setInt(1, getHockeyPlayer().getId().intValue());
            res = stmt.executeQuery();
            if (!res.next()) {
                throw new NoSuchEntityException();
            }

            getHockeyPlayer().setAssists(new Integer(res.getInt("assists")));
            getHockeyPlayer().setBlocks(new Integer(res.getInt("blocks")));
            getHockeyPlayer().setSteals(new Integer(res.getInt("steals")));
            getHockeyPlayer().setPosition(res.getString("position"));
            getHockeyPlayer().setGames(new Integer(res.getInt("games")));
            getHockeyPlayer().setGoals(new Integer(res.getInt("goals")));
            getHockeyPlayer().setMinutes(new Double(res.getDouble("minutes")));
            getHockeyPlayer().setPenaltyMinutes(new Double(res.getDouble("penaltyminutes")));


            super.ejbLoad();

        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                res.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }

    /**
     * @see javax.ejb.EntityBean#ejbStore()
     */
    public void ejbStore() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("update hockeyplayers set assists=?, blocks=?, steals=?, position=?, games=?, goals=?, penaltyminutes=?, minutes=? where id = ?");
            stmt.setInt(1, getHockeyPlayer().getAssists().intValue());
            stmt.setInt(2, getHockeyPlayer().getBlocks().intValue());
            stmt.setInt(3, getHockeyPlayer().getSteals().intValue());
            stmt.setString(4, getHockeyPlayer().getPosition());
            stmt.setInt(5, getHockeyPlayer().getGames().intValue());
            stmt.setInt(6, getHockeyPlayer().getGoals().intValue());
            stmt.setDouble(7, getHockeyPlayer().getPenaltyMinutes().doubleValue());
            stmt.setDouble(8, getHockeyPlayer().getMinutes().doubleValue());
            stmt.setInt(9, getHockeyPlayer().getId().intValue());

            if (stmt.executeUpdate() < 1) {
                throw new NoSuchEntityException();
            }


            super.ejbStore();
        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }

    // </editor-fold>
    public HockeyPlayerBean() {
        setPlayer(new HockeyPlayer());
    }

    public Integer ejbCreate(Player pl) throws CreateException {
        super.ejbCreate(pl);

        setPlayer(pl);

        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("INSERT INTO hockeyplayers (assists, blocks, steals, position, games, goals, penaltyminutes, minutes, id)"
                    + "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
            stmt.setInt(1, getHockeyPlayer().getAssists().intValue());
            stmt.setInt(2, getHockeyPlayer().getBlocks().intValue());
            stmt.setInt(3, getHockeyPlayer().getSteals().intValue());
            stmt.setString(4, getHockeyPlayer().getPosition());
            stmt.setInt(5, getHockeyPlayer().getGames().intValue());
            stmt.setInt(6, getHockeyPlayer().getGoals().intValue());
            stmt.setDouble(7, getHockeyPlayer().getPenaltyMinutes().doubleValue());
            stmt.setDouble(8, getHockeyPlayer().getMinutes().doubleValue());
            stmt.setInt(9, getPlayer().getId().intValue());

            if (stmt.executeUpdate() != 1) {
                throw new CreateException();
            }


            return getHockeyPlayer().getId();

        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }

    public java.lang.Integer ejbFindByPrimaryKey(java.lang.Integer aKey) throws ObjectNotFoundException {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("select id from hockeyplayers where id=?");
            stmt.setInt(1, aKey.intValue());
            res = stmt.executeQuery();

            if (!res.next()) {
                throw new ObjectNotFoundException();
            }


            super.ejbFindByPrimaryKey(aKey);
            return aKey;

        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                res.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }

    public Collection ejbFindAll() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = getDataSource().getConnection();
            stmt = conn.prepareStatement("select id from hockeyplayers");
            res = stmt.executeQuery();

            Vector keys = new Vector();

            while (res.next()) {
                String empno = res.getString(1);
                keys.addElement(empno);
            }


            return keys;

        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                res.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }

    public List ejbHomeSelect(Player plfrom, Player plto) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = getDataSource().getConnection();
            String query = "select * from HOCKEYPLAYERS, PLAYERS where HOCKEYPLAYERS.ID = PLAYERS.ID ";

            List attrs1 = Reflectable.getAttrsNamesFromClass(HockeyPlayer.class);
            for (int i = 0; i < attrs1.size(); ++i) {
                String attr = (String) attrs1.get(i);
                if (plfrom.getAttrType(attr).equals(String.class)) {
                    if (plfrom.getAttrValue(attr) != null) {
                        query += " and HOCKEYPLAYERS." + attr.toUpperCase() + " = '" + plfrom.getAttrValueToStr(attr) + "'";
                    }
                } else {
                    if (plfrom.getAttrValue(attr) != null) {
                        query += " and HOCKEYPLAYERS." + attr.toUpperCase() + " >= " + plfrom.getAttrValueToStr(attr);
                    }
                    if (plto.getAttrValue(attr) != null) {
                        query += " and HOCKEYPLAYERS." + attr.toUpperCase() + " <= " + plto.getAttrValueToStr(attr);
                    }
                }
            }

            List attrs2 = Reflectable.getAttrsNamesFromClass(Player.class);
            for (int i = 0; i < attrs2.size(); ++i) {
                String attr = (String) attrs2.get(i);
                if (plfrom.getAttrType(attr).equals(String.class)) {
                    if (plfrom.getAttrValue(attr) != null) {
                        query += " and PLAYERS." + attr.toUpperCase() + " = '" + plfrom.getAttrValueToStr(attr) + "'";
                    }
                } else {
                    if (plfrom.getAttrValue(attr) != null) {
                        query += " and PLAYERS." + attr.toUpperCase() + " >= " + plfrom.getAttrValueToStr(attr);
                    }
                    if (plto.getAttrValue(attr) != null) {
                        query += " and PLAYERS." + attr.toUpperCase() + " <= " + plto.getAttrValueToStr(attr);
                    }
                }
            }

            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);


            List result = new LinkedList();
            while (rs.next()) {
                HockeyPlayer pl = new HockeyPlayer();
                pl.setPosition(rs.getString("POSITION"));
                pl.setGames(new Integer(rs.getInt("GAMES")));
                pl.setGoals(new Integer(rs.getInt("GOALS")));
                pl.setAssists(new Integer(rs.getInt("ASSISTS")));
                pl.setBlocks(new Integer(rs.getInt("BLOCKS")));
                pl.setSteals(new Integer(rs.getInt("STEALS")));
                pl.setPenaltyMinutes(new Double(rs.getDouble("PENALTYMINUTES")));
                pl.setMinutes(new Double(rs.getDouble("MINUTES")));

                pl.setId(new Integer(rs.getInt("ID")));
                pl.setFirstName(rs.getString("FIRSTNAME"));
                pl.setLastName(rs.getString("LASTNAME"));
                pl.setCountry(rs.getString("COUNTRY"));
                pl.setDateOfBirth(new Integer(rs.getInt("DATEOFBIRTH")));
                pl.setHeight(new Double(rs.getDouble("HEIGHT")));
                pl.setWeight(new Double(rs.getDouble("WEIGHT")));
                pl.setTeamId(new Integer(rs.getInt("TEAMID")));
                result.add(pl);
            }


            return result;

        } catch (SQLException e) {
            throw new EJBException();
        } finally {
            try {
                stmt.close();
                rs.close();
                conn.close();
            } catch (SQLException ex) {
                throw new EJBException();
            }
        }
    }
}
